{% extends 'base.html' %}

{% block title %}
  {% if rate %}编辑汇率{% else %}添加汇率{% endif %} - 数据管理系统
{% endblock %}

{% block breadcrumb %}
  <li class="breadcrumb-item"><a href="{{ url_for('list_currencies') }}">货币管理</a></li>
  <li class="breadcrumb-item"><a href="{{ url_for('list_exchange_rates') }}">汇率管理</a></li>
  <li class="breadcrumb-item active" aria-current="page">{% if rate %}编辑汇率{% else %}添加汇率{% endif %}</li>
{% endblock %}

{% block content %}
  <h2 class="page-header">{% if rate %}编辑汇率{% else %}添加汇率{% endif %}</h2>
  
  <form method="POST">
    <div class="row mb-3">
      <div class="col-md-6">
        <label for="currency_id" class="form-label">货币</label>
        <select class="form-select" id="currency_id" name="currency_id" required>
          <option value="">-- 选择货币 --</option>
          {% for currency in currencies %}
            <option value="{{ currency.id }}" 
              {% if rate and rate.currency_id == currency.id %}selected{% endif %}>
              {{ currency.code }} - {{ currency.name_cn }}
            </option>
          {% endfor %}
        </select>
      </div>
      <div class="col-md-6">
        <label for="base_currency_code" class="form-label">基准货币代码</label>
        <input type="text" class="form-control" id="base_currency_code" name="base_currency_code" 
               value="{{ rate.base_currency_code if rate else 'CNY' }}" required maxlength="3">
      </div>
    </div>
    
    <div class="row mb-3">
      <div class="col-md-4">
        <label for="rate" class="form-label">汇率</label>
        <input type="number" step="0.000001" class="form-control" id="rate" name="rate" 
               value="{{ rate.rate if rate }}" required>
      </div>
      <div class="col-md-4">
        <label for="change_amount" class="form-label">变化金额 (可选)</label>
        <input type="number" step="0.000001" class="form-control" id="change_amount" name="change_amount" 
               value="{{ rate.change_amount if rate and rate.change_amount is not none }}">
      </div>
      <div class="col-md-4">
        <label for="change_percent" class="form-label">变化百分比 (可选)</label>
        <input type="number" step="0.0001" class="form-control" id="change_percent" name="change_percent" 
               value="{{ rate.change_percent if rate and rate.change_percent is not none }}">
      </div>
    </div>
    
    <div class="row mb-3">
      <div class="col-md-6">
        <label for="collected_date" class="form-label">采集日期</label>
        <input type="date" class="form-control" id="collected_date" name="collected_date" 
               value="{{ rate.collected_date.strftime('%Y-%m-%d') if rate else today }}" required>
      </div>
      <div class="col-md-6">
        <label for="source" class="form-label">来源 (可选)</label>
        <input type="text" class="form-control" id="source" name="source" 
               value="{{ rate.source if rate }}">
      </div>
    </div>
    
    <div class="mb-3">
      <label for="data_type" class="form-label">数据类型 (可选)</label>
      <input type="text" class="form-control" id="data_type" name="data_type" 
             value="{{ rate.data_type if rate }}">
    </div>
    
    <button type="submit" class="btn btn-primary">{% if rate %}更新{% else %}添加{% endif %}</button>
    <a href="{{ url_for('list_exchange_rates') }}" class="btn btn-secondary">取消</a>
  </form>
{% endblock %}